﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Settings;
using Utils;

namespace SMSROUTER.Business
{
    public partial class SmsRouter
    {
        #region Singleton Pattern
        private static object _lock = new object();
        private static List<SmsRouter> _instance;
        public static List<SmsRouter> GetInstance()
        {
            lock (_lock)
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        using (var context = new DBDataContext(AppSetting.ConnectionString))
                        {
                            _instance = context.SmsRouters.ToList();
                        }
                    }
                }
            }
            return _instance;
        }
        public static void ReloadData()
        {
            lock (_lock)
            {  
                using (var context = new DBDataContext(AppSetting.ConnectionString))
                {
                    _instance = context.SmsRouters.ToList();
                }
            }
        }
        #endregion Singleton Pattern

        #region Static method

        public static SmsRouter GetByID(int routerId)
        {
            try
            {
                return GetInstance().Find(p => p.RouterID == routerId);
            }
            catch
            {
                return null;
            }
        }

        public static SmsRouter GetByName(string routerName)
        {
            try
            {
                return GetInstance().Find(p => p.RouterName == routerName);
            }
            catch
            {
                return null;
            }
        }

        public static SmsRouter GetByObjectType(string objectType)
        {
            try
            {
                return GetInstance().Find(p => p.TypeOfObject == objectType);
            }
            catch
            {
                return null;
            }
        }

        public static List<SmsRouter> Paging(DBDataContext context, int currentPage, int pageSize, ref int totalRecord, string routerName, string routerType)
        {
            var resutl = context.SmsRouters.AsQueryable();
            if (!string.IsNullOrWhiteSpace(routerName))
            {
                resutl = resutl.Where(p => p.RouterName == routerName);
            }
            if (!string.IsNullOrWhiteSpace(routerType))
            {
                resutl = resutl.Where(p => p.RouterType == routerType);
            }
            totalRecord = resutl.Count();
            return resutl.Skip(((currentPage - 1) < 0 ? 0 : (currentPage - 1)) * pageSize).Take(pageSize).ToList();
        }
        #endregion Static method

        public bool WebSerivceIsAvailable(ref string url_alive)
        {
            url_alive = string.IsNullOrWhiteSpace(RouterURL) ? "" : RouterURL;
            List<string> urls = new List<string>();
            if (!string.IsNullOrEmpty(url_alive))
                urls.Add(url_alive);
            if (!string.IsNullOrWhiteSpace(RouterURL2))
            {
                foreach (string s in RouterURL2.Split(new string[] { ",", ";" }, StringSplitOptions.RemoveEmptyEntries))
                {
                    urls.Add(s.Trim());
                }
            }
            //Check Net, neu dia chi Die thi chuyen sang dia chi moi
            foreach (string url in urls.Distinct())
            {
                if (Common.WebSerivceIsAvailable(url))
                {
                    url_alive = url;
                    return true;
                }
            }

            return false;
        }

        public void Save()
        {
            using (var context = new DBDataContext(AppSetting.ConnectionString))
            {
                if (RouterID == 0)//add
                {
                    context.SmsRouters.InsertOnSubmit(this);
                }
                else //edit
                {
                    var editobj = context.SmsRouters.FirstOrDefault(p => p.RouterID == this.RouterID);
                    if (editobj != null)
                        ObjectClass.AssignValue<SmsRouter>(ref editobj, this);
                }
                context.SubmitChanges();
            }
        }
    }
}
